package gui.showrecipe;

import gui.overview.OverviewPanel;
import gui.editrecipe.*;

import java.awt.GridBagLayout;
import javax.swing.JLabel;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.HeadlessException;
import java.awt.Insets;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JButton;
import org.jdom.JDOMException;

import start.Start;

import xml.XMLRecipeReader;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
 * Frame zur Anzeige eines Rezepts in Seitenformat.
 * @author Dennis Wittkoetter
 *
 */
public class ShowRecipe extends JFrame {
	private static final long serialVersionUID = 1L;
	private ShowRecipe_Content contentPane;
	private JButton btnPrint = new JButton("Drucken");
	private JButton btnBearbeiten = new JButton("bearbeiten", new ImageIcon(Start.getImgs().getEditSmall()));
	private JButton btnFormerRecipe = new JButton(new ImageIcon(Start.getImgs().getMoveBack()));
	private JButton btnNextRecipe = new JButton(new ImageIcon(Start.getImgs().getMoveForward()));
	private JLabel lblCounter = new JLabel();
	private JButton btnClose = new JButton("Schließen", new ImageIcon(Start.getImgs().getCloseSmall()));
	
	static int curSelRow;
	static int nextRecipe;
	static int formerRecipe;
	
	/**
	 * Standard-Konstruktor fuer Rezeptanzeige.
	 * Das Panel ist leer.
	 * @param table	Ursprungstabelle (0=OverviewTable, 1=SearchTable)
	 * @see gui.tables.OverviewTable
	 * @see gui.tables.SearchTable
	 * @throws IOException 
	 * @throws JDOMException 
	 */
	public ShowRecipe(int table) throws JDOMException, IOException {
		this(-1, table);
	}
	/**
	 * Kunstruiert das Panel zur Darstellung eines Rezepts.
	 * Besteht aus einer ScrollPane die eine Panel für das eigentliche Rezept kapselt und mehreren Buttons.
	 * @param selRow	ID des anzuzeigenden Rezepts
	 * @param table	Ursprungstabelle (0=OverviewTable, 1=SearchTable)
	 * @see gui.tables.OverviewTable
	 * @see gui.tables.SearchTable
	 * @throws IOException 
	 * @throws JDOMException 
	 * 
	 */
	public ShowRecipe(int selRow, final int table) throws JDOMException, IOException {
		super("Buchansicht");	//JFrame-Konstruktor aufrufen
		final Dimension d = this.getToolkit().getScreenSize();
		int windowWidth = 900;

		curSelRow = selRow;	//Bugfix
		new XMLRecipeReader(Start.getRezeptexml());
		setLocation((d.width-windowWidth)/2, 0);
		setPreferredSize(new Dimension(windowWidth,d.height-70));
		pack();
		setVisible(true);
		
		/**Layout einstellen*/
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 30, 0, 0, 40, 10, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.2, 0.0, 0.0, 0.3, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		/**ScrollPane zur Kapselung vom JPanel ShowRecipe_Content */
		buildContentPane(selRow, table);
		
		/** Drucken-Button */
		btnPrint.setEnabled(false);
		GridBagConstraints gbc_btnPrint = new GridBagConstraints();
		gbc_btnPrint.insets = new Insets(0, 5, 0, 5);
		gbc_btnPrint.gridx = 0;
		gbc_btnPrint.gridy = 1;
		gbc_btnPrint.anchor = GridBagConstraints.WEST;
		add(btnPrint, gbc_btnPrint);
		
		/** Bearbeiten-Button */
		GridBagConstraints gbc_btnBearbeiten = new GridBagConstraints();
		gbc_btnBearbeiten.anchor = GridBagConstraints.WEST;
		gbc_btnBearbeiten.insets = new Insets(0, 0, 0, 5);
		gbc_btnBearbeiten.gridx = 1;
		gbc_btnBearbeiten.gridy = 1;
		add(btnBearbeiten, gbc_btnBearbeiten);
		btnBearbeiten.addActionListener(new ActionListener() {
			@Override public void actionPerformed(ActionEvent e) {
				try {
					new EditRecipe(curSelRow);
					dispose();
				} catch (HeadlessException e1) {
					e1.printStackTrace();
				} catch (JDOMException e1) {
					e1.printStackTrace();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});
		btnBearbeiten.addKeyListener(new KeyListener() {
			@Override public void keyPressed(KeyEvent e) {	
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					try {
						new EditRecipe(curSelRow);
						dispose();
					} catch (HeadlessException e1) {
						e1.printStackTrace();
					} catch (JDOMException e1) {
						e1.printStackTrace();
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			}
			@Override public void keyReleased(KeyEvent e) {}
			@Override public void keyTyped(KeyEvent e) {}
		});
		
		/** "vorheriges Rezept"-Button */
		GridBagConstraints gbc_btnLastRecipe = new GridBagConstraints();
		gbc_btnLastRecipe.insets = new Insets(0, 0, 0, 5);
		gbc_btnLastRecipe.gridx = 2;
		gbc_btnLastRecipe.gridy = 1;
		add(btnFormerRecipe, gbc_btnLastRecipe);
		btnFormerRecipe.addActionListener(new ActionListener() {
			@Override public void actionPerformed(ActionEvent arg0) {
				try {
					remove(contentPane);
					contentPane.revalidate();
					contentPane.repaint();
					updateContentPane(formerRecipe, table);
					curSelRow--;
				} catch (JDOMException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
		btnFormerRecipe.addKeyListener(new KeyListener() {
			@Override public void keyPressed(KeyEvent arg0) {
				try {
					if (arg0.getKeyCode() == KeyEvent.VK_ENTER) {
						remove(contentPane);
						contentPane.revalidate();
						contentPane.repaint();
						updateContentPane(formerRecipe, table);
						curSelRow--;
					}
				} catch (JDOMException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			@Override public void keyReleased(KeyEvent arg0) {}
			@Override public void keyTyped(KeyEvent arg0) {}
		});
		
		/** "nächstes Rezept"-Button */
		GridBagConstraints gbc_btnNextRecipe = new GridBagConstraints();
		gbc_btnNextRecipe.insets = new Insets(0, 0, 0, 5);
		gbc_btnNextRecipe.gridx = 3;
		gbc_btnNextRecipe.gridy = 1;
		add(btnNextRecipe, gbc_btnNextRecipe);
		btnNextRecipe.addActionListener(new ActionListener() {
			@Override public void actionPerformed(ActionEvent e) {
				try {
					remove(contentPane);
					contentPane.revalidate();
					contentPane.repaint();
					updateContentPane(nextRecipe, table);
					curSelRow++;
				} catch (JDOMException e1) {
					e1.printStackTrace();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});
		btnNextRecipe.addKeyListener(new KeyListener() {
			@Override public void keyPressed(KeyEvent arg0) {
				try {
					if (arg0.getKeyCode() == KeyEvent.VK_ENTER) {
						remove(contentPane);
						contentPane.revalidate();
						contentPane.repaint();
						updateContentPane(nextRecipe, table);
						curSelRow++;
					}
				} catch (JDOMException e1) {
					e1.printStackTrace();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			@Override public void keyReleased(KeyEvent arg0) {}
			@Override public void keyTyped(KeyEvent arg0) {}
		});
		
		/** JLabel Indizierung des aktuellen Rezepts */
		GridBagConstraints gbc_lblCounter = new GridBagConstraints();
		gbc_lblCounter.anchor = GridBagConstraints.EAST;
		gbc_lblCounter.insets = new Insets(0, 0, 0, 5);
		gbc_lblCounter.gridx = 4;
		gbc_lblCounter.gridy = 1;
		add(lblCounter, gbc_lblCounter);
		
		/** Schliessen-Button */
		GridBagConstraints gbc_btnClose = new GridBagConstraints();
		gbc_btnClose.anchor = GridBagConstraints.EAST;
		gbc_btnClose.insets = new Insets(0, 0, 0, 5);
		gbc_btnClose.gridx = 5;
		gbc_btnClose.gridy = 1;
		add(btnClose, gbc_btnClose);
		btnClose.addActionListener(new ActionListener() {
			@Override public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		btnClose.addKeyListener(new KeyListener() {
			@Override public void keyPressed(KeyEvent arg0) {
				if (arg0.getKeyCode() == KeyEvent.VK_ENTER)	
					dispose();
			}
			@Override public void keyReleased(KeyEvent arg0) {}
			@Override public void keyTyped(KeyEvent arg0) {}
		});
	}
	
	
	private void buildContentPane(int selRow, int table) throws JDOMException, IOException {
		formerRecipe = selRow-1;
		nextRecipe = selRow+1;
		
		contentPane = new ShowRecipe_Content(selRow, table);
		contentPane.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		GridBagConstraints gbc_contentPane = new GridBagConstraints();
		gbc_contentPane.gridwidth = 6;
		gbc_contentPane.insets = new Insets(10, 10, 10, 10);
		gbc_contentPane.fill = GridBagConstraints.BOTH;
		gbc_contentPane.gridx = 0;
		gbc_contentPane.gridy = 0;
		add(contentPane, gbc_contentPane);	
		
		if (table == 0)
			setNaviButtonStatus(selRow);
		else {	//Bugfix
			btnFormerRecipe.setVisible(false);
			btnNextRecipe.setVisible(false);
		}
	}

	private void updateContentPane(int selRow, int table) throws JDOMException, IOException {
		formerRecipe = selRow-1;
		nextRecipe = selRow+1;
		
		buildContentPane(selRow, table);
		lblCounter.setText(	 selRow+1+" von "+OverviewPanel.getTable().getTable().getRowCount() );
		
		setNaviButtonStatus(selRow);
	}
	
	private void setNaviButtonStatus(int selRow) {
		if (selRow > 0)
			btnFormerRecipe.setEnabled(true);
		else
			btnFormerRecipe.setEnabled(false);
		
		if (selRow < OverviewPanel.getTable().getTable().getRowCount()-1)
			btnNextRecipe.setEnabled(true);
		else
			btnNextRecipe.setEnabled(false);
	}
}
